<template>
  <view class="invoice-issue-page">
    <image class="page-bg" src="/static/images/bg.png" mode="aspectFill" />
    <view class="gradient-bg" :style="{ top: navBarHeight + 'px' }"></view>
    <CustomNavbar title="发票开具" />
    
    <view class="page-content" :style="{ paddingTop: navBarHeight + 'px' }">
      <!-- 发票详情 -->
      <view class="form-section">
        <view class="section-header">
          <view class="section-line"></view>
          <text class="section-title">发票详情</text>
        </view>
        
        <view class="form-item">
          <text class="form-label">发票详情</text>
          <input 
            class="form-input" 
            v-model="invoiceDetails"
            placeholder="请输入发票详情"
          />
        </view>
        
        <!-- <view class="form-item">
          <text class="form-label">电子发票</text>
          <input 
            class="form-input" 
            v-model="electronicInvoice"
            placeholder="请输入电子发票信息"
            placeholder-style="color: rgba(153, 153, 153, 1)"
          />
        </view> -->
        
        <view class="form-item">
          <text class="form-label">开票金额</text>
          <input 
            class="form-input" 
            v-model="invoiceAmount"
            placeholder="请输入开票金额"
            disabled
          />
        </view>
      </view>
      
      <!-- 底部按钮 -->
      <view class="button-group">
        <button class="cancel-btn" @click="handleCancel">取消</button>
        <button class="confirm-btn" @click="handleConfirm">确定</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import CustomNavbar from '@/components/CustomNavbar.vue'
import { api } from '@/utils/api.js';

const navBarHeight = ref(88)
const invoiceDetails = ref('')
const electronicInvoice = ref('')
const invoiceAmount = ref('168.00')

onLoad((options) => {
  // 接收从发票列表页面传递过来的参数
  if (options.amount) {
    invoiceAmount.value = options.amount
  }
  if (options.billMonth) {
    // 可以根据账单月份设置默认值
  }
  if (options.queryParams) {
    // 可以根据项目设置默认值
    invoiceDetails.value = JSON.parse(decodeURIComponent(options.queryParams))
    console.log('项目详情:', invoiceDetails.value)
  }
})

onMounted(() => {
  const windowInfo = uni.getWindowInfo()
  const statusBarHeight = windowInfo.statusBarHeight || 0
  navBarHeight.value = statusBarHeight + 44
})

const handleCancel = () => {
  uni.navigateBack()
}

const handleConfirm = () => {
  // 验证表单
  // if (!invoiceDetails.value || !electronicInvoice.value) {
  //   uni.showToast({
  //     title: '请填写完整信息',
  //     icon: 'none',
  //     duration: 2000
  //   })
  //   return
  // }
  // wxphone 已由 API 层统一处理，无需手动添加
  
  api.writeInvoice({
    ...invoiceDetails.value,
    amount:invoiceDetails.value.amount*100,
    type: invoiceDetails.value.invoiceType==='月结类'?1000:1100,
    billMonth: invoiceDetails.value.billMonth.replace('年', '').replace('月', '').replace(/\//g, ''),
  }).then(res => {
    // const data = res.data
    console.log('开发票:', JSON.stringify(res))
    if (res.code === 200) {
     
      // 跳转到开票中页面
      uni.redirectTo({
        url: `/pages/invoice-issuing/invoice-issuing?amount=${invoiceAmount.value}&details=${encodeURIComponent(invoiceDetails.value)}&electronic=${encodeURIComponent(electronicInvoice.value)}`,
        success: () => {
          console.log('跳转到开票中页面成功')
        },
        fail: (err) => {
          console.error('跳转失败:', err)
        }
      })
    }else{
      uni.showToast({
        title: res.msg || '开发票成功',
        icon: 'none',
        duration: 2000
      })
    }
    
    
  }).catch(err => {
    console.error('开发票失败:', err)
  })
  
  
}
</script>

<style scoped>
.invoice-issue-page {
   
  position: relative;
  z-index: 1;
  background: rgba(246, 246, 246, 1);
}

.page-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
  pointer-events: none;
  object-fit: cover;
}

.gradient-bg {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  pointer-events: none;
  background: rgba(255, 255, 255, 0.8);
}

.page-content {
  position: relative;
  z-index: 2;
  padding: 40rpx 32rpx;
  padding-bottom: 200rpx;
}

/* 表单区域 */
.form-section {
  /* background: rgba(255, 255, 255, 1); */
  border-radius: 16rpx;
  /* padding: 32rpx; */
  margin-top: 40rpx;
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 32rpx;
}

.section-line {
  width: 8rpx;
  height: 40rpx;
  background: rgba(78, 138, 254, 1);
  border-radius: 4rpx;
  margin-right: 16rpx;
}

.section-title {
  color: rgba(51, 51, 51, 1);
  font-family: HarmonyOS Sans SC;
  font-size: 32rpx;
  font-weight: 700;
  line-height: 38rpx;
}

.form-item {
  margin-bottom: 32rpx;
}

.form-item:last-child {
  margin-bottom: 0;
}

.form-label {
  display: block;
  color: rgba(51, 51, 51, 1);
  font-family: HarmonyOS Sans SC;
  font-size: 28rpx;
  font-weight: 400;
  line-height: 32rpx;
  margin-bottom: 16rpx;
}

.form-input {
  padding: 20rpx;
    background: rgba(246, 246, 246, 1);
    border-radius: 16rpx;
    font-family: HarmonyOS Sans SC;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 32rpx;
    color: rgba(51, 51, 51, 1);
    border: 2rpx solid rgba(204, 204, 204, 1);
}

.form-input[disabled] {
  color: rgba(153, 153, 153, 1);
}

/* 底部按钮 */
.button-group {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  gap: 24rpx;
  padding: 32rpx;
}

.cancel-btn,
.confirm-btn {
  flex: 1;
  height: 88rpx;
  border-radius: 44rpx;
  font-family: HarmonyOS Sans SC;
  font-size: 32rpx;
  font-weight: 500;
  line-height: 88rpx;
  text-align: center;
  border: none;
}

.cancel-btn {
  background: rgba(255, 255, 255, 1);
  color: rgba(78, 138, 254, 1);
  border: 2rpx solid rgba(78, 138, 254, 1);
}

.confirm-btn {
  background: rgba(78, 138, 254, 1);
  color: rgba(255, 255, 255, 1);
}

.cancel-btn::after,
.confirm-btn::after {
  border: none;
}

.cancel-btn:active,
.confirm-btn:active {
  opacity: 0.8;
}
</style>

